package com.LeeCode;

/**
 * K 次修改后的最大曼哈顿距离
 */

public class Code3443 {
    public static void main(String[] args) {
        String s = "NSWWEW";
        int k = 3;
        System.out.println(new Code3443().maxDistance(s, k));
    }

    public int maxDistance(String s, int k) {
        int nNum = 0, sNum = 0, wNum = 0, eNum = 0;
        int ans = 0;
        char[] chars = s.toCharArray();

        for (char c : chars) {
            switch (c) {
                case 'N':
                    nNum++;
                    break;
                case 'S':
                    sNum++;
                    break;
                case 'W':
                    wNum++;
                    break;
                case 'E':
                    eNum++;
                    break;
            }

            int x = Math.abs(nNum - sNum), y = Math.abs(wNum - eNum);
            int flip = Math.min(nNum, sNum) + Math.min(wNum, eNum);
            flip = Math.min(flip, k);
            ans = Math.max(ans, x + y + 2 * flip);
        }
        return ans;
    }
}
